<?php
session_start();
$pageTitle = "分类浏览";
include 'includes/functions.php';

// 获取分类ID
$category_id = isset($_GET['id']) ? intval($_GET['id']) : 0;
$category = getCategoryById($category_id);

if (!$category) {
    header('Location: index.php');
    exit;
}

// 获取该分类下的文章
$articles = getArticlesByCategory($category_id);

// 获取所有分类用于侧边栏
$allCategories = getAllCategories();

include 'includes/header.php';
?>

<div class="container">
    <div class="category-header">
        <h1><?php echo htmlspecialchars($category['name']); ?></h1>
        <?php if (!empty($category['description'])): ?>
            <p class="category-description"><?php echo htmlspecialchars($category['description']); ?></p>
        <?php endif; ?>
        <div class="category-stats">
            <span><i class="fas fa-file-alt"></i> <?php echo count($articles); ?> 篇文章</span>
        </div>
    </div>
    
    <div class="category-layout">
        <div class="category-main">
            <?php if (empty($articles)): ?>
                <div class="empty-state">
                    <i class="fas fa-newspaper"></i>
                    <h3>暂无文章</h3>
                    <p>该分类下还没有文章，成为第一个发布文章的人吧！</p>
                    <?php if (isset($_SESSION['user'])): ?>
                        <a href="create.php" class="btn btn-primary">写第一篇文章</a>
                    <?php else: ?>
                        <a href="login.php" class="btn btn-primary">登录后发布文章</a>
                    <?php endif; ?>
                </div>
            <?php else: ?>
                <div class="blog-grid">
                    <?php foreach ($articles as $article): ?>
                        <div class="blog-card">
                            <div class="card-content">
                                <h3><?php echo htmlspecialchars($article['title']); ?></h3>
                                <p class="card-excerpt"><?php echo htmlspecialchars(mb_substr_safe($article['content'], 0, 100)); ?></p>
                                <div class="card-meta">
                                    <span><i class="far fa-user"></i> <?php echo htmlspecialchars($article['display_name'] ?? $article['author']); ?></span>
                                    <span><i class="far fa-calendar"></i> <?php echo date('Y-m-d', strtotime($article['created_at'])); ?></span>
                                    <span><i class="far fa-eye"></i> <?php echo $article['view_count']; ?></span>
                                </div>
                                <div class="card-actions">
                                    <a href="detail.php?id=<?php echo $article['post_id']; ?>" class="btn btn-small">阅读全文</a>
                                </div>
                            </div>
                        </div>
                    <?php endforeach; ?>
                </div>
            <?php endif; ?>
        </div>
        
        <div class="category-sidebar">
            <div class="sidebar-section">
                <h3>所有分类</h3>
                <div class="category-list">
                    <?php foreach ($allCategories as $cat): ?>
                        <a href="category.php?id=<?php echo $cat['category_id']; ?>" 
                           class="category-item <?php echo $cat['category_id'] == $category_id ? 'active' : ''; ?>">
                            <span class="category-name"><?php echo htmlspecialchars($cat['name']); ?></span>
                            <?php if ($cat['category_id'] == $category_id): ?>
                                <i class="fas fa-chevron-right"></i>
                            <?php endif; ?>
                        </a>
                    <?php endforeach; ?>
                </div>
            </div>
            
            <?php if (isset($_SESSION['user'])): ?>
            <div class="sidebar-section">
                <h3>快速操作</h3>
                <div class="quick-actions">
                    <a href="create.php" class="btn btn-primary btn-block">
                        <i class="fas fa-plus"></i> 写新文章
                    </a>
                    <a href="categories.php" class="btn btn-accent btn-block">
                        <i class="fas fa-tags"></i> 管理分类
                    </a>
                </div>
            </div>
            <?php endif; ?>
        </div>
    </div>
</div>

<?php include 'includes/footer.php'; ?> 